forval i = 1/4{
	import delimited "$dir/Model/simulated_data/simulated_data_rec_dlim_`i'.csv", clear
	
	//renaming
	ren v1 theta
	ren v2 e
	ren v3 x
	ren v4 a
	ren v5 t
	ren v6 p
	ren v7 I
	ren v8 H 
	ren v9 tau
	ren v10 s
	ren v11 s_val
	ren v12 prob_diff_I
	ren v13 prob_diff_H
	ren v14 l 
	ren v15 m
	ren v16 w
	ren v17 tax
	ren v18 c
	ren v19 util
	ren v20 switch
	ren v21 gov_exp
	ren v22 sim
	ren v23 i_v

	
	drop if t == 0 

	//get lifetime wages and years worked for each individual simulation
	preserve
	collapse (sum) w l tax, by(i_v sim)
	tempfile outcomes
	ren w w_total
	ren l l_total
	ren tax tax_total
	save `outcomes'
	restore

	merge m:1 i_v sim using `outcomes', keep(match) nogen

	keep if t == 23 
	gen enroll = (s>1)
	gen hs = (e==1 & s==1)
	gen drop = (e==1 & s>1)
	gen sc = (e==2)
	gen coll = (e==3)

	su enroll hs drop sc coll w_total l_total tax_total
	gen inc_dec = ceil(i_v / 2)
	
	su tau I H if inc_dec == 4 | inc_dec == 5
	tab s if inc_dec == 4 | inc_dec == 5
	su tau if inc_dec == 4 | inc_dec == 5, d

	
	//lifetime wages and years worked, average savings and educadtional attainment, by parent income ventile
	collapse (mean) w_total l_total tax_total enroll hs drop sc coll gov_exp, by (inc_dec)
	ds inc_dec, not
	foreach var in `r(varlist)'{
		ren `var' `var'_rec_d`i'
	}
	save "$temp/outcomes_rec_pinc_dec_dlim_`i'", replace

}

//impact of recession in no-limit world
//impacts of recession over parental income ventiles. And maybe ability, too.
import delimited "$dir/Model/simulated_data/simulated_data_base_nolim.csv", clear

//renaming
ren v1 theta
ren v2 e
ren v3 x
ren v4 a
ren v5 t
ren v6 p
ren v7 I
ren v8 H 
ren v9 tau
ren v10 s
ren v11 s_val
ren v12 prob_diff_I
ren v13 prob_diff_H
ren v14 l 
ren v15 m
ren v16 w
ren v17 tax
ren v18 c
ren v19 util
ren v20 switch
ren v21 gov_exp
ren v22 sim
ren v23 i_v


tab m if l == 1 & t == 18
tab m if l == 1 & t == 20 & e == 2
tab m if l == 1 & t == 22 & e == 3

//nix observations for kids who are in school
drop if t == 0 

//get lifetime wages and years worked for each individual simulation
preserve
collapse (sum) w l tax, by(i_v sim)
tempfile outcomes
ren w w_total
ren l l_total
ren tax tax_total
save `outcomes'
restore

merge m:1 i_v sim using `outcomes', keep(match) nogen

keep if t == 23 
gen enroll = (s>1)
gen hs = (e==1 & s==1)
gen drop = (e==1 & s>1)
gen sc = (e==2)
gen coll = (e==3)

su enroll hs drop sc coll w_total l_total tax_total

gen inc_dec = ceil(i_v / 2)

//lifetime wages and years worked, average savings and educadtional attainment, by parent income ventile
collapse (mean) w_total l_total tax_total enroll hs drop sc coll gov_exp, by (inc_dec)
ds inc_dec, not
foreach var in `r(varlist)'{
	ren `var' `var'_nolim
}
save "$temp/outcomes_rec_pinc_dec_nolim", replace





//get to work!
use "$temp/outcomes_base_pinc_dec", clear

forval i = 1/4{
	merge 1:1 inc_dec using "$temp/outcomes_rec_pinc_dec_dlim_`i'", keep(match) nogen
}

merge 1:1 inc_dec using "$temp/outcomes_rec_pinc_dec_nolim", keep(match) nogen

ds w_total l_total tax_total enroll hs drop sc coll gov_exp
foreach var in `r(varlist)'{
	forval i = 1/2{
		gen `var'_delta_rec_d`i' = `var'_rec_d`i' - `var'
	}
	gen `var'_delta_rec_d3 = `var'_rec_d4 - `var'_nolim
}

forval i = 1/3{
	replace coll_delta_rec_d`i' = coll_delta_rec_d`i' * 100
	replace w_total_delta_rec_d`i' = w_total_delta_rec_d`i' * 40000
	replace w_total_delta_rec_d`i' = w_total_delta_rec_d`i' / 1000
}


//lifetime wage impacts in thousands of dollars

save "$temp/rec_impacts_dlim", replace


twoway connected coll_delta_rec_d2 inc_dec, ylabel(-5(2.5)10) yline(0) xlabel(1(1)10) ///
xtitle("Parent Income Decile") ytitle("Change in College Attainment (pp)") color(black) msize(medsmall) msymbol(circle) ///
|| connected coll_delta_rec_d1 inc_dec, color(black) msize(medsmall) msymbol(triangle) lp(dash) || connected coll_delta_rec_d3 inc_dec, color(black) msize(medsmall) msymbol(square) lp(dot) ///
legend(lab(1 "Baseline") lab(2 "No Change in Limit") lab(3 "No Borrowing Limit"))
graph export "$output/recession_impacts_coll_dlim.png", replace

twoway connected w_total_delta_rec_d2 inc_dec, ylabel(-50(25)100) yline(0) xlabel(1(1)10) ///
xtitle("Parent Income Decile") ytitle("Change in Lifetime Wages ($1000)") color(black) msize(medsmall) msymbol(circle) ///
|| connected w_total_delta_rec_d1 inc_dec, color(black) msize(medsmall) msymbol(triangle) lp(dash) || connected w_total_delta_rec_d3 inc_dec, color(black) msize(medsmall) msymbol(square) lp(dot) ///
legend(lab(1 "Baseline") lab(2 "No Change in Limit") lab(3 "No Borrowing Limit"))
graph export "$output/recession_impacts_w_dlim.png", replace
graph close




